﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HitchHikersGuideToTheGalaxy.Models
{
    public class FuelRepository : HitchHikersGuideToTheGalaxy.Models.IFuelRepository
    {
        GalaxyDataContext db = new GalaxyDataContext();

        #region Default Add, Delete and save actions

        /// <summary>
        /// Add new Fuel record to database
        /// </summary>
        /// <param name="fuel"></param>
        public void Add(Fuel fuel)
        {
            db.Fuels.InsertOnSubmit(fuel);
        }

        /// <summary>
        /// Delete one Fuel record from database
        /// </summary>
        /// <param name="fuel"></param>
        public void Delete(Fuel fuel)
        {
            db.Fuels.DeleteOnSubmit(fuel);
        }

        /// <summary>
        /// Commit changes made to database
        /// </summary>
        public void Save()
        {
            db.SubmitChanges();
        }

        #endregion

        #region Querys

        /// <summary>
        /// Fetch all Fuel records from database
        /// </summary>
        /// <returns>Fuel collection</returns>
        public IQueryable<Fuel> FindAllFuels()
        {
            return db.Fuels;
        }

        /// <summary>
        /// Get one Fuel record from database
        /// </summary>
        /// <param name="id">ID of fuel</param>
        /// <returns>Fuel by Id</returns>
        public Fuel GetFuel(int id)
        {
            return db.Fuels.SingleOrDefault(d => d.Id == id);
        }

        /// <summary>
        /// Get the fuel type id
        /// </summary>
        /// <param name="type">String with fuel name</param>
        /// <returns>Id of fuel</returns>
        public int GetFuelId(string type)
        {
            return (from a in db.Fuels
                   where a.Type == type
                   select a.Id).SingleOrDefault();
        }

        /// <summary>
        /// Calculate Tour cost
        /// </summary>
        /// <param name="price"></param>
        /// <param name="distance"></param>
        /// <param name="usageCombined"></param>
        /// <returns></returns>
        public decimal CalculateTourCost(decimal price, decimal distance, decimal usageCombined)
        {
            try 
            {
                return Decimal.Multiply(Decimal.Multiply(Decimal.Divide(usageCombined, 100), distance), price);
            } 
            catch
            {
                return Decimal.Zero;
            }
        }

        /// <summary>
        /// Returns fuel price
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public decimal GetFuelPrice(int? id)
        {
            if (id.Equals(null)) 
            {
                return Decimal.Zero;
            }

            decimal price = (from f in db.Fuels
                             where f.Id == id
                             select f.Price).SingleOrDefault();
            return price;
        }

        /// <summary>
        /// Divide Tour cost among passengers.
        /// </summary>
        /// <param name="cost"></param>
        /// <param name="passengers"></param>
        /// <returns></returns>
        public decimal DivideTourCostAmongPassengers(decimal cost, int passengers)
        {
            return Decimal.Divide(cost, Convert.ToDecimal(passengers));
        }

        #endregion
    }
}
